package com.wf.course.web.controller.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.utils.dateUtil.DateUtil;
import com.utils.page.PageBean;
import com.wf.course.model.user.SysMenu;
import com.wf.course.model.user.User;
import com.wf.course.service.ISysMenuService;
import com.wf.course.web.Constants;
/**
 * 
 * @content 系统菜单Controller
 */
@RequestMapping("sysMenu")
@Controller
public class SysMenuController {

	@Autowired
	private ISysMenuService sysMenuService;

	/**
	 * 列表展示
	 * @param pageNo
	 * @param pageSize
	 * @param menuName
	 * @param menuLevel
	 * @param model
	 * @return
	 */
	@RequestMapping("sysMenuList")
	public String adList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "menuName", required = false) String menuName, 
			@RequestParam(value = "menuLevel", required = false) String menuLevel, 
			Model model) {
		
		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		if(menuName!=null && menuName!=""){
			cons.put("menuName", menuName);
		}
		if(menuLevel!=null && menuLevel!=""){
			cons.put("menuLevel", menuLevel);
		}
		PageBean<SysMenu> pageBean = new PageBean<SysMenu>(pageNo, pageSize, null, cons);
		pageBean.setUrl("sysMenu/sysMenuList");
		this.sysMenuService.indexInfo(pageBean);
		model.addAttribute("page", pageBean);
		
		return "sys/sysMenuList";
	}

	/**
	 *  修改和新增页面数据初始化
	 * 
	 * @param menuId
	 * @param model
	 * @return
	 */
	@RequestMapping("pageLoad/{menuId}")
	public String downLoadById(@PathVariable("menuId") Integer menuId, Model model) {
		 
		if (menuId != null && menuId != 0) {
			SysMenu sysMenu = sysMenuService.querySysMenuById(menuId);
			model.addAttribute("sysMenu", sysMenu);
			model.addAttribute("type", "upd");
		}else{
			model.addAttribute("type", "add");
		}
		
		 List<SysMenu> menus = sysMenuService.querySysMenuList();
		 model.addAttribute("menus",menus);
		
		return "sys/sysMenuEdit";
	}

	/**
	 * 新增和修改
	 * @param sysMenu
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "sysMenuEdit" , produces = "text/html;charset=UTF-8")
	public String sysMenuEdit(@ModelAttribute SysMenu sysMenu,HttpServletRequest request) {
		
		//获取创建人
		Object token = request.getSession().getAttribute("login_token");
		User user = Constants.LOGIN_USER.get(token);
		sysMenu.setCreateUser(user.getId());
		//获取系统当前时间
		sysMenu.setCreateTime(DateUtil.getCurrentTime());
		//保存或修改
		sysMenuService.saveOrUpdateSysMenu(sysMenu);
		
		return "redirect:/sysMenu/sysMenuList";
	}
	

}
